/*
 * 文件名：BPSAspect.java
 * 版权：Copyright 2006-2011 AsiaInfo Tech. Co. Ltd. All Rights Reserved. 
 * 描述： BPSAspect.java
 * 修改人：Administrator
 * 修改时间：2011-8-11
 * 修改内容：新增
 */
package com.fengxing.ams.web.common;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;

import com.fengxing.ams.pub.AMSException;
import com.fengxing.ams.pub.constants.ReltCode;
import com.fengxing.ams.pub.dto.PubInfoDTO;
import com.fengxing.ams.pub.dto.ResultDTO;
import com.fengxing.ams.pub.manager.dao.ILogDAO;
import com.fengxing.ams.pub.manager.intf.IConfigManager;
import com.fengxing.ams.pub.util.ErrorDescUtil;
import com.fengxing.ams.pub.util.IOUtil;
import com.fengxing.ams.pub.util.LogUtil;
import com.fengxing.ams.pub.vo.LogVO;

/**
 * BPSAspect实现类
 * 
 * @author Administrator
 * @version CTMS V100R001 2011-8-11
 * @since CTMS V100R001C01
 */
public class BPSCdrAspect implements Ordered {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(BPSCdrAspect.class);

	private IContextManager contextManager;

	private IConfigManager configManager;

	private ILogDAO logDAO;

	@Resource
	public void setConfigManager(IConfigManager configManager) {
		this.configManager = configManager;
	}

	/**
	 * 设置contextManager
	 * 
	 * @param contextManager
	 *            要设置的contextManager
	 */
	@Resource
	public void setContextManager(IContextManager contextManager) {
		this.contextManager = contextManager;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getOrder() {
		return 0;
	}

	/**
	 * 可以再此处做一些鉴权处理和日志打印
	 * 
	 * @param pjp
	 *            ProceedingJoinPoint对象
	 * @return Object 对象
	 */
	public Object doAspect(ProceedingJoinPoint pjp) {
		if (logger.isDebugEnabled()) {
			LogUtil.debug(logger, "doAspect(ProceedingJoinPoint) - start" + pjp); //$NON-NLS-1$
		}

		ResultDTO resultObj = null;

		Object retVal = null;
		Long reltCode = ReltCode.SUCESS_CODE;
		String reltDesc = null;
		try {
			retVal = pjp.proceed();
		} catch (Throwable e) {
			logger.error("doAspect(ProceedingJoinPoint)", e); //$NON-NLS-1$

			LogVO logVO = this.contextManager.getCdrVO();

			String errMsg = IOUtil.getStrackFromThrowable(e);

			if (errMsg.length() > 2000) {
				errMsg = errMsg.substring(0, 2000);
			}

			logVO.setErrMsg(errMsg);

			for (Throwable ex = e; ex != null; ex = ex.getCause()) {
				if (ex instanceof AMSException) {
					AMSException ctmsException = (AMSException) e;
					resultObj = ctmsException.getResultVO();
					reltCode = resultObj.getCode(); // ex.403004
					throw (RuntimeException) e;
				} else {
					reltCode = ReltCode.SERVER_INTERNALIS_ERROR;
					reltDesc = MessageFormat.format(ErrorDescUtil
							.getDescByCode(ReltCode.SERVER_INTERNALIS_ERROR), e
							.getMessage());
					throw (RuntimeException) e;
				}
			}
		} finally {
			LogVO logVO = this.contextManager.getCdrVO();

			Long startTime = logVO.getStartTime();
			long endTime = System.currentTimeMillis();
			long stayTime = endTime - startTime;

//			logVO.setModuleID(this.configManager.getProperty(
//					SystemParam.MOUDLE_ID, "1000"));
			logVO.setEndTime(endTime);
			logVO.setStayTime((int) stayTime);
			logVO.setReltCode(reltCode);
			logVO.setReltDesc(reltDesc);
			logVO.setLogId(this.contextManager.getRequestID());
			logVO.setMethodName(this.contextManager.getRequestInfo()
					.getMethodName());
			PubInfoDTO pubInfoDTO = this.contextManager.getPubInfo();
			if (pubInfoDTO != null) {
				logVO.setAppId(pubInfoDTO.getAppId());
				logVO.setClientIP(pubInfoDTO.getClientIP());
				logVO.setCountyCode(pubInfoDTO.getCountyCode());
				logVO.setRegionCode(pubInfoDTO.getRegionCode());
				logVO.setTranscationId(pubInfoDTO.getTransactionId());
				logVO.setTranscationTime(pubInfoDTO.getTransactionTime());
			}
//			this.logService.saveCdr(logVO);
		}

		if (logger.isDebugEnabled()) {
			LogUtil.debug(logger, "doAspect(ProceedingJoinPoint) - end"); //$NON-NLS-1$
		}
		return retVal;
	}
}
